//
//  ZFStaticCollectionView.swift
//  map
//
//  Created by fyzq on 2024/1/31.
//

import UIKit

class ZFStaticCollectionConfig{
    var row:Int = 4;
    var item_height:CGFloat = 50.0;
    var insets:UIEdgeInsets = UIEdgeInsets.zero
    var minimumLineSpacing:CGFloat = 0;///滚动方向的间距
    var minimumInteritemSpacing:CGFloat = 0;///滚动方向的间距
    var radius:CGFloat = 0;
}
//
//class ZFCollectionBaseConfig{
//    var space:CGFloat = 5;
//    var inset:UIEdgeInsets = UIEdgeInsets.zero
//    var backgroundColor:UIColor = UIColor.clear
//    var radius:CGFloat = 5;
//}
//
//class ZFCollectionTextConfig : ZFCollectionBaseConfig{
//    var titleFont:UIFont = UIFont.iconfont(size: 15)
//    var titleTextColor:UIColor = UIColor.darkGray
//
//
//}
//
//class ZFCollectionImageConfig : ZFCollectionBaseConfig{
//    var size:CGSize = CGSize(width: 24, height: 24);
//    var contentMode:UIView.ContentMode = UIView.ContentMode.scaleAspectFit
//    
//}

protocol ZFStaticCollectionDataSource {

    func collectionView(collectionView: ZFStaticCollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    func collectionView(collectionView: ZFStaticCollectionView, numberOfItemsInSection section: Int) -> Int
    func collectionView(collectionView: ZFStaticCollectionView) -> Int
    
}



class ZFStaticCollectionView: UIView {
    private var collectionView:UICollectionView!
    var config:ZFStaticCollectionConfig = ZFStaticCollectionConfig()
    var dataSource:ZFStaticCollectionDataSource?
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setZFCollectionView();
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    fileprivate func setZFCollectionView(){
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
    
        layout.itemSize = CGSize.init(width: 0, height: 0);
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout);
        collectionView.delegate = self;
        collectionView.dataSource = self;
        self.addSubview(collectionView);
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        collectionView.backgroundColor = .clear
    }
    public func register(cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String){
        collectionView.register(cellClass, forCellWithReuseIdentifier: identifier)
    }
    
    public func reloadData(){
        collectionView.reloadData()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        let inset = collectionView.contentInset;
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout;
        
        layout.minimumLineSpacing = config.minimumLineSpacing
        layout.minimumInteritemSpacing = config.minimumInteritemSpacing
        
        let allCount  = self.dataSource?.collectionView(collectionView: self) ?? 0
        
        var count = allCount / config.row + 1;
        if count % config.row  == 0 {
            count -= 1;
        }
        let item_width = (CGRectGetWidth(frame) - inset.left - inset.right - CGFloat(count - 1) * config.minimumInteritemSpacing ) / CGFloat(config.row);
        layout.itemSize = CGSize.init(width: item_width, height: config.item_height)
        collectionView.contentInset = config.insets;
        collectionView.snp.remakeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(CGFloat(count) * config.item_height + config.insets.top + config.insets.bottom + CGFloat(count - 1) * config.minimumLineSpacing);
        }
    }
}


extension ZFStaticCollectionView:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return self.dataSource?.collectionView(collectionView: self, numberOfItemsInSection: section) ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = (dataSource?.collectionView(collectionView: self, cellForItemAt: indexPath))! as  UICollectionViewCell
        return cell;
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    }
}
